﻿Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Text
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls

Namespace DotNetNuke.Modules.ActiveForums.Controls
    <DefaultProperty("Text"), ToolboxData("<{0}:Link runat=server></{0}:Link>")> _
    Public Class Link
        Inherits WebControl
        Private _text As String
        Private _visible As Boolean
        Private _navigateUrl As String
        Private _enabledRoles As String
        Private _userRoles As String
        Private _authRequired As Boolean
        Private _pageId As Integer
        Private _params As String
        Private _title As String
        Public Property NavigateURL() As String
            Get
                Return _navigateUrl
            End Get
            Set(ByVal value As String)
                _navigateUrl = value
            End Set
        End Property
        Public Property EnabledRoles() As String
            Get
                Return _enabledRoles
            End Get
            Set(ByVal value As String)
                _enabledRoles = value
            End Set
        End Property
        Public Property UserRoles() As String
            Get
                Return _userRoles
            End Get
            Set(ByVal value As String)
                _userRoles = value
            End Set
        End Property
        Public Property AuthRequired() As Boolean
            Get
                Return _authRequired
            End Get
            Set(ByVal value As Boolean)
                _authRequired = value
            End Set
        End Property
        <Bindable(True), Category("Appearance"), DefaultValue(""), Localizable(True)> Property Text() As String
            Get
                Return _text
            End Get
            Set(ByVal Value As String)
                _text = Value
            End Set
        End Property
        Public Property PageId() As Integer
            Get
                Return _pageId
            End Get
            Set(ByVal value As Integer)
                _pageId = value
            End Set
        End Property
        Public Property Params() As String
            Get
                Return _params
            End Get
            Set(ByVal Value As String)
                _params = Value
            End Set
        End Property
        Public Property Title() As String
            Get
                Return _title
            End Get
            Set(ByVal value As String)
                _title = value
            End Set
        End Property

        Protected Overrides Sub Render(ByVal writer As HtmlTextWriter)
            If Not String.IsNullOrEmpty(EnabledRoles) Then
                If String.IsNullOrEmpty(UserRoles) Then
                    Visible = False
                Else
                    Visible = Permissions.HasAccess(EnabledRoles, UserRoles)
                End If
            End If
            If AuthRequired And Not HttpContext.Current.Request.IsAuthenticated Then
                Visible = False
            End If
            If String.IsNullOrEmpty(NavigateURL) And String.IsNullOrEmpty(Params) Then
                NavigateURL = Utilities.NavigateUrl(PageId)
            ElseIf String.IsNullOrEmpty(NavigateURL) And Not String.IsNullOrEmpty(Params) Then
                NavigateURL = Utilities.NavigateUrl(PageId, "", Params.Split(CChar(",")))
            End If
            Dim sTitle As String = " title="
            If Not String.IsNullOrEmpty(Title) Then
                sTitle &= """" & Title & """"
            Else
                sTitle = String.Empty
            End If
            Dim sClass As String = String.Empty
            If Not String.IsNullOrEmpty(CssClass) Then
                sClass = " class=""" & CssClass & """"
            End If
            If Visible Then
                If Enabled Then
                    writer.Write("<a href=""" & NavigateURL & """" & sTitle & sClass & ">")
                End If
                writer.Write(Text)
                If Enabled Then
                    writer.Write("</a>")
                End If
            End If

        End Sub

        Private Sub Link_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
            EnableViewState = False
        End Sub
    End Class

End Namespace
